package com.sgy.springmvc_demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@Controller
@RequestMapping("/restful")
public class RestfulController {

    @RequestMapping(value = "/handle01/{id}", method = RequestMethod.GET)
    public ModelAndView handle01(@PathVariable("id") Integer id) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        // addObject相当于向请求域中request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/handle02", method = RequestMethod.POST)
    public ModelAndView handle02(@RequestParam("username") String username) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        // addObject相当于向请求域中request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/handle03", method = RequestMethod.PUT)
    public ModelAndView handle03(@RequestParam("username") String username) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        // addObject相当于向请求域中request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/handle04/{id}", method = RequestMethod.DELETE)
    public ModelAndView handle04(@PathVariable("id") Integer id) {
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        // addObject相当于向请求域中request.setAttribute("date", date)
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }
}
